package com.tianqu.tms.www.member.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;
/**
 * 进入会员信息录入页Action
 * 
 * @author tianshifu
 *
 */
public class MemberCreationInputAction extends TmsAction {
	
	private Logger logger = Logger.getLogger(MemberCreationInputAction.class);
	
	private static final String ONE = "1";
	private static final String TWO = "2";
	private static final String THREE = "3";

	private static final String IMGLIST = "imgList";//图片list
	
	private static final String TOCLEAR = "toClear";//清空填入内容 放入一个标识字段证明进入到确认页面了
	private static final String FLAG = "flag";
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		//返回的时候需要从uc中获取填入的信息和图片信息，第一次是空跳
		
		MemberInputViewHelper view = new MemberInputViewHelper();
		String flag = "";
		if(!StringUtil.isNullOrBlank((String)uc.getAttribute(FLAG))){
			flag = (String)uc.getAttribute(FLAG);
		}
		
		
		if("1".equals(flag))
		{
		
			if(uc.getAttribute(TmsConstants.VIEWHELP)!=null)
			{
				view =(MemberInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			}
			
			//从mapList中取出图片信息set到viewhelp中
			List<CarImage> imgList = new ArrayList<CarImage>();
			if(uc.getAttribute(IMGLIST)!=null)
			{
				imgList = (List<CarImage>)uc.getAttribute(IMGLIST);
			}
			if(imgList!=null)
			{
				//将list中的数据取出set到viewhelp中
				for(int i=0;i<imgList.size();i++)
				{
					CarImage entity = (CarImage)imgList.get(i);
					if(entity.getImgKey().equals(TmsConstants.ONE))
					{
						view.setOrgPhoto(entity.getImgPath());
					}
					if(entity.getImgKey().equals(TmsConstants.TWO))
					{
						view.setBusinessPhoto(entity.getImgPath());
					}
					if(entity.getImgKey().equals(TmsConstants.THREE))
					{
						view.setCardPhoto(entity.getImgPath());
					}
					if(entity.getImgKey().equals(TmsConstants.FIVE))
					{
						view.setMemberZipCode(entity.getImgPath());
					}
				}
				
			}else{
				//在list为空的情况下赋一个初值
				view.setOrgPhoto(TmsConstants.BLANK);
				view.setBusinessPhoto(TmsConstants.BLANK);
				view.setCardPhoto(TmsConstants.BLANK);
				view.setMemberZipCode(TmsConstants.BLANK);
			}
		}else{
			uc.removeAttribute(IMGLIST);
			uc.removeAttribute(TmsConstants.VIEWHELP);
		}
		if(uc.getAttribute(FLAG)!=null){
			uc.removeAttribute(FLAG);
		}
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}


}
